/**
 * SoundManager Class
 * Copyright (c) 2009 designmarco
 * @author Marco Di Giuseppe | http://designmarco.com
 * marco[at]designmarco[dot]com
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.designmarco.managers
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.utils.Dictionary;
	/**
	 * SoundManager provides an easy way to play audio stored in the FLAs Library.
	 * @example
		<code>
		import com.designmarco.utils.SoundManager;<br/><br/>
			
		SoundManager.getInstance().registerSound(new ClickSound(), "click"); <br/>
		btnA.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler, false, 0, true); <br/><br/>
				
		function mouseHandler(event:MouseEvent):void<br/>
		{
			SoundManager.getInstance().playSound("click");<br/>
		}
		</code>
	 */
	public class SoundManager
	{
		private var sounds:Dictionary;
		private var channels:Dictionary;
		private var musicChannel:SoundChannel;
		private var musicTransform:SoundTransform;
		private var soundTransform:SoundTransform;
		private static var instance:SoundManager;
		private static var allowInstantiation:Boolean;

		public function SoundManager()
		{
			if (!allowInstantiation)
					throw new Error("Error, Already An Instance");
			else
			{
				instance 		= this;
				sounds			= new Dictionary();
				channels 		= new Dictionary();
				musicChannel 	= new SoundChannel();
				musicTransform 	= new SoundTransform(1);
				soundTransform 	= new SoundTransform(1);
			}
		}

		/**
		 * Registers Sound with Manager
		 * @param	sound	sound instance from the library
		 * @param	id		unique id to identify sound
		 */
		public function registerSound(sound:Sound, id:String):void
		{
			sounds[id] = sound;
		}

		/**
		 * Start Playing Sound
		 * @param	id			 unique id to identify sound
		 * @param	startTime	 starting point of sound
		 * @param	loops		 number of times for sound to repeat
		 */
		public function playSound(id:String, startTime:Number = 0, loops:int = 0):void
		{
			if(!sounds[id])
				throw new Error("Sound: " + id + " does not exist!");
			else
				channels[id] = sounds[id].play(startTime, loops, soundTransform);
		}

		/**
		 * Stop Playing Sound
		 * @param	id		sound id
		 */
		public function stopSound(id:String):void
		{
			if(channels[id])
			{
				channels[id].stop();
				delete channels[id];
			}
		}

		/**
		 * Start Playing Music
		 * @param	id		unique id to identify song
		 * @param	start	starting point of song
		 * @param	loops   number of play cycles
		 */
		public function playMusic(id:String, startTime:Number = 0, loops:int = 0):void
		{
			if(!sounds[id])
				throw new Error("Music: " + id + " does not exist!");
			else
				musicChannel = sounds[id].play(startTime, loops, musicTransform);
		}

		/**
		 * Stop Playing Music
		 */
		public function stopMusic():void
		{
			musicChannel.stop();
		}

		/**
		 * Determine Volume Level of Sound
		 * @return volume
		 */
		public function get soundVolume():Number
		{
			return soundTransform.volume;
		}

		/**
		 * Set Sound Volume
		 * @param value		sound volume level
		 */
		public function set soundVolume(value:Number):void
		{
			if (value < 0) value = 0;
			soundTransform.volume = value;
		}

		/**
		 * Determine Volume level of Music
		 * @return music volume
		 */
		public function get musicVolume():Number
		{
			return musicTransform.volume;
		}

		/**
		 * Set Music Volume level
		 * @param value   music volume
		 */
		public function set musicVolume(value:Number):void
		{
			if (value < 0) value = 0;
			musicTransform.volume = value;
			musicChannel.soundTransform = musicTransform;
		}
		
		/**
		 * SoundManager Entry Point
		 * @return SoundManager Instance
		 */
		public static function getInstance():SoundManager
		{
			if (!instance)
			{
				allowInstantiation 	= true;
				instance = new SoundManager();
				allowInstantiation 	= false;
			}
			return instance;
		}
	}
}